package com.infovane.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;



import com.infovane.base.BaseAction;
import com.infovane.model.TArticleStyle;
import com.infovane.service.ArticleStyleService;
import com.infovane.utils.CheckFileType;
import com.infovane.utils.SystemConstant;

public class ArticleStyleAction extends BaseAction{
	
	private ArticleStyleService articleStyleService;
	
	
	public ArticleStyleService getArticleStyleService() {
		return articleStyleService;
	}


	public void setArticleStyleService(ArticleStyleService articleStyleService) {
		this.articleStyleService = articleStyleService;
	}
	private File myFile;
    private String fileName;
    private String imageFileName;

   

    public void setMyFileFileName(String fileName) {
        this .fileName = fileName;
   } 
       
    public void setMyFile(File myFile) {
        this .myFile = myFile;
   } 
   
    public String getImageFileName() {
        return imageFileName;
   }
    
    /**
	 * 把源文件对象复制成目标文件对象
	 */
	private static void copy(File src,File dst){
		InputStream in=null;
		OutputStream out=null;
		try{
		in=new BufferedInputStream(new FileInputStream(src),SystemConstant.BUFFER_SIZE); 
		out=new BufferedOutputStream(new FileOutputStream(dst),SystemConstant.BUFFER_SIZE);
		byte[] buffer= new byte[SystemConstant.BUFFER_SIZE];
		int len=0;
		while((len=in.read(buffer))>0){
			out.write(buffer, 0, len);
		}
		}catch(Exception e){
			logger.info("upload failed!",e);
		}finally{
			if(in!=null){
				try{
				in.close();
				in=null;
				}catch(IOException e){
					logger.info("in close failed!",e);
				}
			}
			if(out!=null){
				try{
				out.close();
				out=null;
				}catch(IOException e){
					logger.info("out close failed!",e);
				}
			}
		}
	}
   
	/**
	 *创建资讯分类样式
	 * */
	public String create(){
		String typename=httpServletRequest.getParameter("typename");
		String stylename=httpServletRequest.getParameter("stylename");
		String styledescribe=httpServletRequest.getParameter("styledescribe");
		if(typename==null||typename.equals("")){
			String warn="资讯大分类不能为空！";
			httpServletRequest.setAttribute("WARN",warn);
			return ERROR;
		}else if(stylename==null||stylename.equals("")){
			String warn="资讯小分类名称不能为空！";
			httpServletRequest.setAttribute("WARN",warn);
			return ERROR;
		}else if(styledescribe==null||styledescribe.equals("")){
			String warn="资讯小分类描述不能为空！";
			httpServletRequest.setAttribute("WARN",warn);
			return ERROR;
		}else{
			String imagePath="not";
			if(myFile!=null){
				if(CheckFileType.isPic(fileName)){
					imageFileName=new Date().getTime()+CheckFileType.getExtention(fileName);
					File imageFile=new File(ServletActionContext.getServletContext().getRealPath("/images/icons")+"/"+imageFileName);
					imagePath="/images/icons/"+imageFileName;
					copy(myFile,imageFile);
					
				}
			}
			TArticleStyle tArticleStyle=new TArticleStyle();
			tArticleStyle.setArticleTypeName(typename);
			tArticleStyle.setArticleStyleName(stylename);
			tArticleStyle.setArticleStyleImage(imagePath);
			tArticleStyle.setArticleStyleDescribe(styledescribe);
			boolean sign=this.articleStyleService.add(tArticleStyle);
			if(!sign){
				String warn="不可预知错误！";
				httpServletRequest.setAttribute("WARN",warn);
				return ERROR;
			}
		}
		return SUCCESS;
	}
	
	/**
	 * 获取资讯所有小类别
	 * */
	public String getAll(){
		List<TArticleStyle> articleStype=new ArrayList<TArticleStyle>();
		articleStype=this.articleStyleService.getAll();
		if(articleStype.size()>0){
			httpServletRequest.setAttribute("ARTICLESTYLE", articleStype);
			return SUCCESS;
		}
		return ERROR;
	}
	
	/**
	 * 删除资料小类别
	 * */
	public String delete(){
		int id=Integer.parseInt(httpServletRequest.getParameter("id"));
		boolean sign=this.articleStyleService.delete(TArticleStyle.class, id);
		if(!sign){
			String warn="删除操作出错!";
			httpServletRequest.setAttribute("WARN",warn);
			return ERROR;
		}
		return SUCCESS;
		
	}
	
	/**
	 * 获取资讯小类别对象
	 * */
	public String get(){
		int id=Integer.parseInt(httpServletRequest.getParameter("id"));
		TArticleStyle articleStyle=new TArticleStyle();
		articleStyle=(TArticleStyle)this.articleStyleService.get(TArticleStyle.class, id);
		if(articleStyle==null){
			String warn="获取数据出错!";
			httpServletRequest.setAttribute("WARN", warn);
			return ERROR;
		}
		httpServletRequest.setAttribute("TARTICLESTYLE", articleStyle);
		return SUCCESS;
	}
	
	/**
	 * 更新（修改）资讯小类别
	 * */
	public String update(){
		int styleid=Integer.parseInt(httpServletRequest.getParameter("styleid"));
		String typename=httpServletRequest.getParameter("typename");
		String stylename=httpServletRequest.getParameter("stylename");
		String styledescribe=httpServletRequest.getParameter("styledescribe");
		if(typename==null||stylename==null||styledescribe==null){
			String warn="请输入数据!";
			httpServletRequest.setAttribute("WARN", warn);
			return ERROR;
		}else{
			TArticleStyle articleStyle=new TArticleStyle();
			articleStyle.setArticleStyleId(styleid);
			articleStyle.setArticleTypeName(typename);
			articleStyle.setArticleStyleName(stylename);
			articleStyle.setArticleStyleDescribe(styledescribe);
			boolean sign=this.articleStyleService.update(articleStyle);
			if(!sign){
				String warn="不可知错误!";
				httpServletRequest.setAttribute("WARN", warn);
				return ERROR;
			}
			return SUCCESS;
		}
		
	}
}
